<?php
// +----------------------------------------------------------------------
// | Fengli [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.yanyongwang.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 风黎 < 1280246895@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use app\common\model\QuestionBank;
use app\common\model\Questions;
use \think\Cache;
use \think\Controller;
use think\Loader;
use think\Db;
use \think\Cookie;
use app\admin\controller\Permissions;
use think\Validate;

class Phpspreadsheet extends Permissions
{
    public function uploadQuestion()
    {
        if(!request()->file('file'))        return "未上传文件";

        $inputFileName      =  $_FILES['file']['tmp_name'];

        $spreadsheet        = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);


        $sheetData          = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
        $qb_id              = Db::name('question_bank')->insertGetId(['qb_name'=>$sheetData['2']['A'],'create_time'=>time()]);
        unset($sheetData['1']);
        unset($sheetData['2']);
        unset($sheetData['3']);
        $questions          = [];
        foreach($sheetData as $k=>$v){

            $questions[$k]['question']      = $v['A'];
            $questions[$k]['A']             = $v['B'];
            $questions[$k]['B']             = $v['C'];
            $questions[$k]['C']             = $v['D'];
            $questions[$k]['D']             = $v['E'];
            $questions[$k]['answer']        = $v['F'];
            $questions[$k]['qb_id']         = $qb_id;
        }
        Db::name('questions')->insertAll($questions);
        $data   = ['code'=>'200','msg'=>'题库导入成功'];
        return json($data);
    }
}